package com.key.w8.http1.endpoint;

import com.key.w8.common.execute.ChainExecuteHandler;
import com.key.w8.http1.connection.Connection;
import com.key.w8.http1.connection.ConnectionBuilder;
import com.key.w8.http1.connection.connectionImpl.ServerHttp1PersistentConnection;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: k8
 * @CreateTime: 2025-03-08
 * @Version: 1.0
 */
public class ServerConnectionBuilder implements ConnectionBuilder<ServerHttp1PersistentConnection,Object> {
    private List<ChainExecuteHandler> extendExecuteHandlers = new LinkedList<>();
    private List<ChainExecuteHandler> extendErrorHandlers = new LinkedList<>();
    private List<Connection.CloseListener> closeListeners = new LinkedList<>();
    private List<Connection.ErrorListener> errorListeners = new LinkedList<>();
    private List<Connection.CreatedListener> createdListeners = new LinkedList<>();
    public ServerHttp1PersistentConnection build(){
        ServerHttp1PersistentConnection connection = new ServerHttp1PersistentConnection();
        extendExecuteHandlers.forEach(connection::addChainExecuteHandler);
        extendErrorHandlers.forEach(connection::addChainErrorHandler);
        closeListeners.forEach(connection::addCloseListener);
        errorListeners.forEach(connection::addErrorListener);
        createdListeners.forEach(connection::addCreatedListener);
        return connection;
    }

    @Override
    public void addExecuteHandler(ChainExecuteHandler handler) {
        this.extendExecuteHandlers.add(handler);
    }

    @Override
    public void addExecuteErrorHandler(ChainExecuteHandler handler) {
        this.extendErrorHandlers.add(handler);
    }

    @Override
    public void addErrorListener(Connection.ErrorListener errorListener) {
        assert errorListener != null;
        errorListeners.add(errorListener);
    }

    @Override
    public void addCloseListener(Connection.CloseListener closeListener) {
        assert closeListener != null;
        closeListeners.add(closeListener);
    }

    @Override
    public void addCreatedListener(Connection.CreatedListener createdListener) {
        assert createdListener != null;
        createdListeners.add(createdListener);
    }


}
